package com.cb.controller.member.order;

import java.awt.Color;
import java.awt.Font;
import java.awt.image.BufferedImage;
import java.util.*;
import java.awt.Graphics;
import java.io.IOException;
//import java.io.PrintWriter;
import javax.imageio.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class VCodeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public VCodeServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		HttpSession session=request.getSession();
		try{
			int codeLength=4;//��֤�볤��
			int mixTimes=5;//ģ��̶Ȳ���
			Color bgColor=getRandColor(200, 250);//������ɫ
			Color bfColor=new Color(0,0,0);//������ɫ
			boolean ifRandomColor=true;//�����ַ��Ƿ���ɫ���
			boolean ifMixColor=true;//ģ�����Ƿ���ɫ���
			String [] fontName={"Bell MT", "MS Reference Sans Serif", "Comic Sans MS",
					"Kristen ITC", "Constantia", "Georgia", "Lucida Calligraphy",
					"Times New Roman", "Fixedsys", "Consolas"};

			//����ҳ�治����
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);
			// ���ڴ��д���ͼ��
			int width = 15*codeLength, height = 20;
			BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
			// ��ȡͼ��������
			Graphics g = image.getGraphics();
			// �趨����ɫ
			g.setColor(bgColor);
			g.fillRect(0, 0, width, height);
			//�趨����
			g.setFont(new Font(fontName[random.nextInt(fontName.length)], Font.PLAIN, 20));
			//System.out.println("��VCodeServlet.java�ڣ������ǣ�"+g.getFont().getFontName());
			//���߿�
			g.setColor(new Color(33,66,99));
			g.drawRect(0,0,width-1,height-1);
			// ����������ߣ�ʹͼ���е���֤�벻�ױ��������̽�⵽
			g.setColor(getRandColor(160, 200));
			for (int i = 0; i < mixTimes*codeLength/10; i++) {
				for (byte j=0;j<10;j++){
					if (ifMixColor)
				 		g.setColor(getRandColor(160, 200));
					int x = random.nextInt(width);
					int y = random.nextInt(height);
					int xl = random.nextInt(12);
					int yl = random.nextInt(12);
					g.drawLine(x, y, x + xl, y + yl);
				}
				g.drawLine(1, 1, width-1, height-1);
				g.drawLine(1, height-1, width-1, 1);
				g.setColor(getRandColor(20,110,0));		//������ɫ�����ײ�����
				g.drawLine(1, height/2+2, width, height/2+2);
			}
			// ȡ���������֤��(codeLengthλ����)
			String sRand = "";
			String rand;
			String letters="abcdefghjkmnpqrstuvwxyziI10oO23456789ABCDEFGHJKLMNPQRSTUVWXYZ;',./-=[]";
			for (int i = 0; i < codeLength; i++) {
				int l=random.nextInt(letters.length());
				rand = letters.substring(l,l+1);
				sRand = sRand+rand;
				// ����֤����ʾ��ͼ����
				if (ifRandomColor)
					g.setColor(getRandColor(20,110,0)); 
				else
					g.setColor(bfColor);
			 	//���ú����4����ɫ��ͬ����������Ϊ����̫�ӽ�����ֻ��ֱ�����
				g.drawString(rand, 13 * i + 6, 16);
			}
			// ����֤�����SESSION
			session.setAttribute("vcode", sRand);
			//System.out.println(this.getServletName()+", "+this.getClass()+", "
			//			+this.getClass().getName()+"��ɵ���֤�룺"+sRand);	//����ȡ������ķ���
			// ͼ����Ч
			g.dispose();
			// ���ͼ��ҳ��
			ImageIO.write(image, "PNG", response.getOutputStream());
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
//	��Χ��������ɫ
	private static Random random=new Random();
	private Color getRandColor(int fc, int bc) {
		return getRandColor(fc, bc, fc);
	}
	private Color getRandColor(int fc, int bc,int interval) {
		if (fc > 255) {
			fc = 255;
		}
		if (bc > 255) {
			bc = 255;
		}
		int r = fc + random.nextInt(bc - interval);
		int g = fc + random.nextInt(bc - interval);
		int b = fc + random.nextInt(bc - interval);
		return new Color(r, g, b);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}
}